/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package uimigration.UIModel.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import uimigration.UIModel.Container;
import uimigration.UIModel.LayoutType;
import uimigration.UIModel.UIElement;
import uimigration.UIModel.UIModelPackage;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Container</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link uimigration.UIModel.impl.ContainerImpl#getLayout <em>Layout</em>}</li>
 *   <li>{@link uimigration.UIModel.impl.ContainerImpl#isIsRoot <em>Is Root</em>}</li>
 *   <li>{@link uimigration.UIModel.impl.ContainerImpl#getContainsUIElement <em>Contains UI Element</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */

public class ContainerImpl extends UIElementImpl implements Container {
	/**
	 * The default value of the '{@link #getLayout() <em>Layout</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLayout()
	 * @generated
	 * @ordered
	 */
	protected static final LayoutType LAYOUT_EDEFAULT = LayoutType.LEFT_TO_RIGHT;

	/**
	 * The cached value of the '{@link #getLayout() <em>Layout</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLayout()
	 * @generated
	 * @ordered
	 */
	protected LayoutType layout = LAYOUT_EDEFAULT;

	/**
	 * The default value of the '{@link #isIsRoot() <em>Is Root</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isIsRoot()
	 * @generated
	 * @ordered
	 */
	protected static final boolean IS_ROOT_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isIsRoot() <em>Is Root</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isIsRoot()
	 * @generated
	 * @ordered
	 */
	protected boolean isRoot = IS_ROOT_EDEFAULT;

	/**
	 * The cached value of the '{@link #getContainsUIElement() <em>Contains UI Element</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getContainsUIElement()
	 * @generated NOT
	 * @ordered
	 */
	protected EList<UIElement> containsUIElement;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ContainerImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return UIModelPackage.Literals.CONTAINER;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public LayoutType getLayout() {
		return layout;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setLayout(LayoutType newLayout) {
		LayoutType oldLayout = layout;
		layout = newLayout == null ? LAYOUT_EDEFAULT : newLayout;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, UIModelPackage.CONTAINER__LAYOUT, oldLayout, layout));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isIsRoot() {
		return isRoot;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setIsRoot(boolean newIsRoot) {
		boolean oldIsRoot = isRoot;
		isRoot = newIsRoot;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, UIModelPackage.CONTAINER__IS_ROOT, oldIsRoot, isRoot));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<UIElement> getContainsUIElement() {
		if (containsUIElement == null) {
			containsUIElement = new EObjectContainmentEList<UIElement>(
					UIElement.class, this,
					UIModelPackage.CONTAINER__CONTAINS_UI_ELEMENT);
		}
		return containsUIElement;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public NotificationChain eInverseRemove(final InternalEObject otherEnd,
			final int featureID, final NotificationChain msgs) {
		switch (featureID) {
		case UIModelPackage.CONTAINER__CONTAINS_UI_ELEMENT:
			return ((InternalEList<?>) getContainsUIElement()).basicRemove(
					otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case UIModelPackage.CONTAINER__LAYOUT:
				return getLayout();
			case UIModelPackage.CONTAINER__IS_ROOT:
				return isIsRoot();
			case UIModelPackage.CONTAINER__CONTAINS_UI_ELEMENT:
				return getContainsUIElement();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings("unchecked")
	public void eSet(final int featureID, final Object newValue) {
		switch (featureID) {
		case UIModelPackage.CONTAINER__LAYOUT:
			setLayout((LayoutType) newValue);
			return;
		case UIModelPackage.CONTAINER__IS_ROOT:
			setIsRoot(((Boolean) newValue).booleanValue());
			return;
		case UIModelPackage.CONTAINER__CONTAINS_UI_ELEMENT:
			getContainsUIElement().clear();
			getContainsUIElement().addAll((Collection<UIElement>) newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case UIModelPackage.CONTAINER__LAYOUT:
				setLayout(LAYOUT_EDEFAULT);
				return;
			case UIModelPackage.CONTAINER__IS_ROOT:
				setIsRoot(IS_ROOT_EDEFAULT);
				return;
			case UIModelPackage.CONTAINER__CONTAINS_UI_ELEMENT:
				getContainsUIElement().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case UIModelPackage.CONTAINER__LAYOUT:
				return layout != LAYOUT_EDEFAULT;
			case UIModelPackage.CONTAINER__IS_ROOT:
				return isRoot != IS_ROOT_EDEFAULT;
			case UIModelPackage.CONTAINER__CONTAINS_UI_ELEMENT:
				return containsUIElement != null && !containsUIElement.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (layout: ");
		result.append(layout);
		result.append(", isRoot: ");
		result.append(isRoot);
		result.append(')');
		return result.toString();
	}

	@Override
	public UIElement findUIElement(int Id) {
		int n=getContainsUIElement().size();
		int i=0;
		if(this.id==Id)
			return this;
		if(n>0)
			while(i<n && getContainsUIElement().get(i).getId()!=Id)
			{
				try {
					Container c=(Container) (( Container)getContainsUIElement().get(i)).findUIElement(Id);
					if(c!=null)
						return c;
				} catch (ClassCastException e) {
					//e.printStackTrace();
				}
				i++;
			}
		if(i<n){
			//System.out.println("findElement: "+getContainsUIElement(i));
			return getContainsUIElement().get(i);
		}
		return null;
	}

} //ContainerImpl
